package cn.rongdao.jrkabao.util;

import com.mifengkong.frtools.util.FRCollection;
import com.mifengkong.frtools.util.FRString;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by jack on 2017/8/7
 */

public class NumberUtil {

    /**
     * 对数字进行分割，比如6000000将会分割成6,000,000
     *
     * @param num
     * @return
     */
    public static String splitNum(int num) {
        BigDecimal bd = new BigDecimal(num);
        return parseMoney(",###,###", bd);

    }

    public static String parseMoney(String pattern, BigDecimal bd) {
        DecimalFormat df = new DecimalFormat(pattern);
        return df.format(bd);
    }

    public static String getTwoDecimalPlaces(float decimal) {
        DecimalFormat df = new DecimalFormat("######0.00");
        return FRString.valueOf(df.format(decimal));
    }

    public static String getTwoDecimalPlaces(String decimal) {
        if (FRString.isEmpty(decimal)) {
            return "0.00";
        }
        DecimalFormat df = new DecimalFormat("######0.00");
        return FRString.valueOf(df.format(Float.valueOf(decimal)));
    }

    public static String getNumberFromString(String str) {
        if (FRString.isEmpty(str)) {
            return FRString.EMPTY_STRING;
        }
        String regEx = "[^0-9]";
        Pattern p = Pattern.compile(regEx);
        Matcher m = p.matcher(str);
        return m.replaceAll("").trim();
    }

    public static String getStringFromNumber(String number) {
        if (FRString.isEmpty(number)) {
            return FRString.EMPTY_STRING;
        }
        return FRCollection.appendAsString("每月", number, "日", "");
    }

    public static String getTwoDecimalNumber(double d) {
        Double dd = d * 100;
        d = Math.ceil(dd) / 100;
        DecimalFormat df = new DecimalFormat("######0.00");
        return FRString.valueOf(df.format(d));
    }
}
